//
//  MXSNewfateVC.swift
//  weibo
//
//  Created by 孙飞 on 15/12/10.
//  Copyright © 2015年 Sunfei. All rights reserved.
//

import UIKit

class MXSNewfateVC: UIViewController, UIScrollViewDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        setupUI()
        
    }
    
    /// 视图初始化
    private func setupUI(){
        
        scrollView.frame = view.bounds
        view.addSubview(scrollView)
        pageControl.frame = CGRect(x: scrollView.frame.width * 0.5  , y: scrollView.frame.height - 100, width: 0, height: 0)
        view.addSubview(pageControl)
        
        //循环添加图片
        let count = 4
        for i in 0..<count {
            
            let imageView = UIImageView(image: UIImage(named: "new_feature_\(i + 1)"))
            
            imageView.frame = CGRect(x: CGFloat(i) * scrollView.frame.width, y: 0, width: scrollView.frame.width, height: scrollView.frame.height)
            scrollView.addSubview(imageView)
            
            // 最后一页 布置按钮点击进入住页面
            if i == count-1 {
                setupLastPage(imageView)
            }
        }
        
        scrollView.contentSize = CGSize(width: CGFloat(count) * scrollView.frame.width, height: 0)
        
        pageControl.numberOfPages = count
        
    }
    
    
    //最后一页
    private func setupLastPage(lastPage: UIView){
        
        //图片上的按钮 开启响应属性
        lastPage.userInteractionEnabled = true
        enterButton.center = CGPoint(x: lastPage.frame.width * 0.5, y: lastPage.frame.height - 150)
        
        lastPage.addSubview(enterButton)
        
    }
    
    // MARK: - 按钮点击事件
    @objc private func sharedButtonClick(button: UIButton){
        button.selected = !button.selected
    }
    
    @objc private func enterButtonClick(){
        
        print("点击进入按钮")
        NSNotificationCenter.defaultCenter().postNotificationName(MXSSwitchRootVCNotifation, object: nil)
    }
    
    // MARK: - scrollView delegate
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        // 加 0.5 达到计算 四舍五入的效果
        let page = scrollView.contentOffset.x / scrollView.frame.width + 0.5
        
        pageControl.currentPage = Int(page)
    }
    
    // MARK: - 懒加载控件
    private lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        
        scrollView.delegate = self
        scrollView.pagingEnabled = true
        scrollView.bounces = false
        scrollView.showsHorizontalScrollIndicator = false
        
        return scrollView
    }()
    
    
    
    // 分页控件
    private lazy var pageControl: UIPageControl = {
        let pageControl = UIPageControl()
        
        // 选中颜色
        pageControl.currentPageIndicatorTintColor = UIColor.orangeColor()
        
        pageControl.pageIndicatorTintColor = UIColor.darkGrayColor()
        return pageControl
    }()
    
    // 按钮
    private lazy var enterButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "enterButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        //图片
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setTitle("进入微博", forState: UIControlState.Normal)
        
        button.sizeToFit()
        return button
    }()
    
    
}